#####
## Replication File:
##
## "The 2020 Presidential Election and Beliefs 
## About Fraud: Continuity or Change?"
##
## Published in Electoral Studies
#####

################################################################################

####
## Load necessary packages
## 
## Analyses conducted in RStudio 1.3.1093,
## using R 3.6 (Mac OS)
####

library(stats)
library(car)
library(lattice)
library(latticeExtra)
library(foreign)
library(psych)
library(ggplot2)
library(ggthemes)

# Set working directory

################################################################################

####
## Figure 1
####

time <- read.csv("Over Time.csv", header = TRUE)

prop <- subset(time, group==1)
corr <- subset(time, group==2)

plot1 <- xyplot(estimate ~ year,
       data = prop,
       aspect = 0.75,
       pch = c(16),
       ylab = "Proportion",
       xlab = "Election Year",
       main = "A. Proportion Likely/Very Likely",
       col = "black",
       ylim = c(0.15, 0.6),
       panel = function(x, y, ...){
               panel.xyplot(x, y, col=...)
               panel.segments(prop$year, prop$lower, 
                              prop$year, prop$upper)
       }
)

plot2 <- xyplot(estimate ~ year,
       data = corr,
       aspect = 0.75,
       pch = c(16),
       ylab = "Correlation Coefficient",
       xlab = "Election Year",
       main = "B. Correlation with Partisanship",
       col = "black",
       ylim = c(-0.25, 0.33),
       panel = function(x, y, ...){
               panel.xyplot(x, y, col=...)
               panel.segments(corr$year, corr$lower, 
                              corr$year, corr$upper)
               panel.abline(h=0, col = "dark grey", lty = 2)
       }
)

tiff("figure1.tiff", units = "in", height = 4, width = 8, res=300, compression = 'lzw')
print(plot1, position = c(0, 0, .5, 1), more = TRUE)
print(plot2, position = c(.5, 0, 1, 1), more = FALSE)
dev.off()

##########################################################################

####
## Figure 2
####

coeff <- read.csv("Standardized Coefficients.csv", header = TRUE)

coeff$iv <- reorder(coeff$iv, -coeff$order)

coeff$dv <- reorder(coeff$dv, coeff$group)

tiff("figure2.tiff", units = "in", height = 6.5, width = 8.5, res=300, compression = 'lzw')
xyplot(iv ~ estimate | dv,
       data = coeff,
       aspect = 1.25,
       ylab = "",
       xlab = "Standardized Coefficient",
       col = "black",
       pch = 20,
       xlim = c(-0.53, 0.48),
       scond = as.numeric(coeff$dv),
       panel = function(x, y, scond, subscripts, ...){
               pnl = panel.number()
               panel.abline(v=0, lty=2, col = "dark grey")
               panel.xyplot(x, y, col=...)
               panel.segments(coeff$lower[scond==pnl], coeff$iv[scond==pnl], 
                              coeff$upper[scond==pnl], coeff$iv[scond==pnl])
       }
)
dev.off()

################################################################################

####
## Figure A1 
####

preds <- read.csv("Predictions.csv", header = TRUE)

preds$order <- factor(preds$order,
                      levels = c(1,2,3,4,5,6,7,8,9),
                      labels = c("Anomie", "Conspiracy Think", "Dark Triad",
                                 "Denialism", "Social Media", "Republican",
                                 "Conservative", "Party Strength", "Ideo. Strength"))

preds$dv <- factor(preds$dv,
                   levels = c("Cancel Election", "Leave Office", "Mail-in Ballots",
                              "Post Office", "Voter Fraud"),
                   labels = c("Cancel Election", "Leave Office", "Mail-in Ballots",
                              "Post Office", "General Fraud"))

tiff("figureA1.tiff", units = "in", height = 9, width = 14, res=300, compression = 'lzw')
useOuterStrips(xyplot(estimate ~ scale | order + dv,
       data = preds,
       aspect = 1,
       ylab = "Predicted Value",
       xlab = " ",
       col = "black",
       pch = 20,
       type = c("p","l"),
       ylim = c(.75, 5),
       scond = preds$group,
       panel = function(x, y, scond, subscripts, ...){
         pnl = panel.number()
         panel.xyplot(x, y, col=...)
         panel.segments(preds$scale[scond==pnl], preds$lower[scond==pnl], 
                        preds$scale[scond==pnl], preds$upper[scond==pnl])
       }
       )
       )
dev.off()


